package cn.b.sky.flowable.service.impl;

import cn.b.sky.flowable.dao.ProcessModelParamDao;
import cn.b.sky.flowable.model.ProcessModelParam;
import cn.b.sky.flowable.service.ProcessParamService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("processParamService")
public class ProcessParamServiceImpl implements ProcessParamService {

  @Autowired
  private ProcessModelParamDao paramDao;

  @Resource
  private ECodeService eCodeService;

  @Override
  public List<ProcessModelParam> getProcessModeParamByModelId(String modelId) {
    if (StringUtils.isBlank(modelId)) {
      return null;
    }
    List<ProcessModelParam> pmps = paramDao.getProcessModelParamsByModelId(modelId);
    for (ProcessModelParam pmp : pmps) {
      ECode ecode = eCodeService.selectEcodeByECodeTypeAndLanguageCodeAndECode(pmp.getDataType(),
          ECodeEnums.PARAMS_DATATYPE.getCode(), SecurityContextUtil.getLanguageCode());
      pmp.setDataTypeText(ecode.geteCodeText());
    }
    return pmps;
  }

  @Override
  public void saveProcessModel(ProcessModelParam param) {
    if (param != null && !StringUtils.isBlank(param.getModelId()) && !StringUtils.isBlank(param.getCode())) {
      paramDao.insertProcessModelParams(param);
    }
  }

  @Override
  public void deleteProcessModelByPK(String modelId, String code) {
    if (!StringUtils.isBlank(modelId) && !StringUtils.isBlank(code)) {
      ProcessModelParam param = new ProcessModelParam(code, modelId);
      paramDao.delProcessModelParams(param);
    }

  }

  @Override
  public ProcessModelParam getProcessModeParamByModelIdAndCode(String modelId, String code) {
    if (StringUtils.isBlank(modelId) || StringUtils.isBlank(code)) {
      return null;
    }
    return paramDao.getProcessModeParamByModelIdAndCode(modelId, code);
  }

}
